/**  
 * @Title: ModelController.java
 * @Package com.xzit.tmms.controller.task
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午8:50:49
 * @version V1.0  
 */
package com.xzit.tmms.controller.task;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.xzit.tmms.bean.task.Model;
import com.xzit.tmms.common.base.BaseController;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.exception.UtilException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.common.util.FileUtil;
import com.xzit.tmms.service.task.ModelService;

/**
 * @ClassName: ModelController
 * @Description: TODO
 * @author 秦冲
 * @date 2016年6月27日 上午8:50:49
 */
@Controller
@RequestMapping("/model")
public class ModelController extends BaseController {

	@Resource
	private ModelService modelService;

	@RequestMapping("")
	public String index() {

		return "model/model-index";
	}

	/**
	 * @throws ServiceException
	 * @Title: queryModel
	 * @Description: TODO 查询模板
	 */
	@RequestMapping("/queryModel")
	public String queryModel(ModelMap mm) throws ServiceException {
		Page<Map<String, Object>> page = new Page<>(getPageIndex(), getPageSize());
		mm.addAttribute("page", modelService.queryModel(page));
		return "model/model-query";
	}

	/**
	 * @throws UtilException
	 * @Title: addModel
	 * @Description: TODO模板添加
	 */
	@RequestMapping("/addModel")
	public String addModel() throws UtilException {

		return "model/model-add";
	}
/**
 * @Title: addModelSubmit
 * @Description: 确认增加
 * @throws  ServiceException 
 */
	@RequestMapping("/addModelSubmit")
	public String addModelSubmit(Model model, MultipartFile modelFile) throws UtilException, ServiceException {
		// 文件保存
		if (modelFile != null) {
			model.setModelPath(FileUtil.saveFile(modelFile));
		}
		modelService.addModel(model);
		return "redirect:/model.action";
	}

	/**
	 * @throws ServiceException 
	 * @Title: updateModel
	 * @Description: TODO 模板修改
	 */
	@RequestMapping("/updateModel")
	public String updateModel(ModelMap model,Integer modelId) throws ServiceException {
		Model m=modelService.getModelById(modelId);
		model.addAttribute("m",m);
		return "model/model-update";
	}
	/**
	 * @throws ServiceException 
	 * @Title: updateModelSubmit
	 * @Description: TODO 模板修改确认
	 */
	@RequestMapping("/updateModelSubmit")
	public String updateModelSubmit(Model model,MultipartFile modelFile) throws ServiceException, UtilException{
		// 文件保存
				if (modelFile != null) {
					model.setModelPath(FileUtil.saveFile(modelFile));
				}
		modelService.updateModel(model);
		return "redirect:/model.action";
	}
	/**
	 * @throws ServiceException 
	 * @Title: deleteModel
	 * @Description: TODO 模板删除
	 */
	@RequestMapping("/deleteModel")
	public String deleteModel(Integer modelId) throws ServiceException{
		modelService.deleteModel(modelId);
		return "redirect:/model.action";
	}
}
